
(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
        typeof define === 'function' && define.amd ? define(factory) :
            (global.VueBrowserLoader = factory(global));
}(this, (function (env) {
    'use strict';
    var Vue = env.Vue, //依赖vue
        doc = env.document;

    var lib = new Vue();
    lib.opts = {
        scriptType:'text/vue',
        exports:env,
        exportsName:'vue',
    };
    lib.ready = false;

    lib.init = function(){
        return new Promise(function (y, n) {
            if(lib.ready){
                y();
            }
            else {
                lib.$on('onReady',function () {
                    y();
                })
            }
        })
    };
    lib.get = function (url,opts) {
        return new Promise(function (y,n) {
            opts = opts || {};
            opts.url = url;
            opts.async = opts.async || true;
            opts.json = opts.json || false;
            var req = new XMLHttpRequest();
            req.onreadystatechange = function() {
                if(req.readyState === 4){
                    if (req.status >= 200 && req.status < 400) {
                        y({
                           data: opts.json?JSON.parse(req.responseText) : req.responseText ,
                            opts:opts
                        });
                    }
                    else {
                        n(url)
                    }
                }
            };
            req.onerror = function(err) {
                n(err);
            };
            req.open('GET', url , opts.async);
            req.send('');
        });

    };
    var div = doc.createElement('div');
    lib.parseTagsText = function (tagsText, scriptEl) {
        div.innerHTML = tagsText;
        div.querySelectorAll('tag').forEach(function (tag) {
            eval(tag.querySelector('script').innerHTML);
            var comp = lib.opts.exports[lib.opts.exportsName];
            if(comp){
                comp.template = tag.querySelector('template').innerHTML;
                Vue.component(tag.getAttribute('name'),comp);
            }
            else {
                console.error('未正确导出',scriptEl,tag);
            }
            lib.opts.exports[lib.opts.exportsName] = null;
        });
    };

    env.addEventListener('load',function () {
       var allScript = doc.querySelectorAll('[type="'+lib.opts.scriptType+'"]');
       var allGet = [],i,j,len;
        for ( i = 0,len = allScript.length; i < len; i++) {
            var script = allScript[i];
            allGet.push(lib.get(script.src,{
                index:i,
                el:script,
            }))
        }

        Promise.all(allGet).then(function (arr) {
            var resArr = new Array(arr.length);
            var res; //根据加载状态排序
            arr.forEach(function (res) {
                resArr[res.opts.index] = res;
            });
            resArr.forEach(function (v) {
                lib.parseTagsText(v.data,v.opts.el);
            });
            lib.$emit('onReady');
        })

    });

    return lib;
})));
